﻿using System;
using System.Collections.Generic;
using System.Linq;
using CookComputing.XmlRpc;

namespace BloggerAutoPosterService.Engine.WordPress
{
    public class MetaWeblogClient : XmlRpcClientProtocol
    {
        #region Properties (7)

        public string BlogUrl { set; get; }

        public string Password { set; get; }

        public bool PostAsDraft { set; get; }

        public string PostBody { set; get; }

        public IList<string> PostTags { set; get; }

        public string PostTitle { set; get; }

        public string UserName { set; get; }

        #endregion Properties

        #region Methods (2)

        // Public Methods (2) 

        [XmlRpcMethod("metaWeblog.newPost")]
        //this should be lowercase too
        public string newPost(int blogId, string username, string password, Post content, bool publish)
        {
            return (string)this.Invoke("newPost", new object[] { blogId, username, password, content, publish });
        }

        public void PostNewEntry()
        {
            this.Url = BlogUrl;
            var post = new Post
            {
                dateCreated = DateTime.Now,
                title = PostTitle,
                description = PostBody,
                categories = PostTags.ToArray()
            };
            newPost(1, UserName, Password, post, publish: !PostAsDraft);
        }

        #endregion Methods
    }
}
